<?php

namespace api\modules\v1\models;
use Yii;
use api\common\models\Rate;
use api\common\models\Mar;
use yii\helpers\Url;
use yii\web\NotFoundHttpException;
class Commentpc extends Comment
{


    /*
 * 评论提交
 * @return bool
 * @throws NotFoundHttpException
 */
    public function pl(){
        if ($this->validate()) {
            $user = $this->getTokenUserId();
            if($user =='') {
                throw new NotFoundHttpException('access-token错误.');
            }

            $news_table = '{{%'.News::getTable($this->catid).'}}';
            $news = new Mar($news_table);
            if($this->pid != 0){ //针对评论进行评论
                //self::updateAll(['comment' => new Expression('`comment` + 1')], ['comment_id' => $this->pid]);

                $uid = self::findOne(['comment_id' => $this->pid]);
                $msg = new Msg();
                $msg->take_uid = $uid->uid;
                $msg->send_uid = $user['id'];
                $msg->message = $uid->content;
                $msg->type = 2;
                $msg->url = Url::to(['comment/list','content_id' => $this->content_id,'catid' => $this->catid,'pid' => $this->pid], false);
                $msg->time = date('Y-m-d H:i:s');
                $msg->isread = 0;
                $msg->insert();

            }else{ //针对文章进行评论
                //$news->updateAll(['comment' => new Expression('`comment` + 1')], ['id' => $this->content_id]);
            }

            $this->uid = $user['id'];
            $this->username = $user['nickname'] != '' ? urldecode($user['nickname']) : $user['phone'];
            $this->time = date('Y-m-d H:i:s');
            if ($this->save(false)) {
                return true;
            }
        }
        return null;
    }
}
